package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.wanho.entity.Student;

import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author：汤小洋
 * Date：2024-04-01 10:22
 * Description：<描述>
 */
@RestController
@RequestMapping("/doc")
@RequiredArgsConstructor
@Slf4j
public class DocController {

    private final ElasticsearchRestTemplate restTemplate;

    /**
     * 创建文档
     */
    @GetMapping("/create")
    public String create(){
        restTemplate.save(new Student(1, "张三", 18, "男", "清华大学"));
        return "success";
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Student findById(@PathVariable Integer id){
        Student student = restTemplate.get(id.toString(), Student.class);
        return student;
    }

    /**
     * 批量创建文档
     */
    @GetMapping("/batch")
    public String batch(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(2, "李四", 19, "男", "北京大学"));
        list.add(new Student(3, "王五", 20, "男", "复旦大学"));
        list.add(new Student(4, "赵六", 21, "男", "上海交通大学"));
        list.add(new Student(5, "小红", 22, "女", "南京大学"));
        list.add(new Student(6, "小明", 23, "男", "浙江大学"));
        list.add(new Student(7, "小刚", 24, "男", "武汉大学"));
        list.add(new Student(8, "小丽", 25, "女", "中山大学"));
        restTemplate.save(list);
        return "success";
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public List<Student> findAll(){
        SearchHits<Student> hits = restTemplate.search(Query.findAll(), Student.class);

        System.out.println(hits.getTotalHits());
        List<Student> list = hits.stream()
                .map(hit -> hit.getContent())
                .collect(Collectors.toList());
        return list;
    }

    /**
     * 按条件查询
     */
    @GetMapping("/search")
    public List<Student> search(){
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("school", "南京"))
                .build();

        SearchHits<Student> hits = restTemplate.search(query, Student.class);

        System.out.println(hits.getTotalHits());
        List<Student> list = hits.stream()
                .map(hit -> hit.getContent())
                .collect(Collectors.toList());


        try {
            log.info("开始执行除法运算。。。。。。。");
            System.out.println(5/0);
        } catch (Exception e) {
            log.error("程序出现异常：" + e.getMessage());
            throw new RuntimeException(e);
        }


        return list;
    }



}
